# coding:utf8
from __future__ import unicode_literals

from django.db import models

# Create your models here.
from cmdb.models import Server


from myauth.models import MyGroup, MyUser, PlatForm
#sorted()
STATUS_CHOICE = (
    (-1, '未安装'),
    (0, '已经安装'),
    (1, '安装中'),
    (2, '已经合服'),
    (3, '特殊状态'),
)


class Mysql(models.Model):
    MASTER = 'master'
    SLAVE = 'standby'
    ARCHITECTURE_CHOICES = (
        (MASTER, '主数据库'),
        (SLAVE, '从数据库')
    )
    server = models.ForeignKey(Server, verbose_name='服务器')
    port = models.IntegerField('端口', default=3306)
    mysql_type = models.CharField(
        verbose_name='mysql类型', choices=ARCHITECTURE_CHOICES, max_length=100)
    master = models.ForeignKey('self', verbose_name='主数据库', blank=True, null=True,
                               limit_choices_to={'mysql_type': 'master'})
    db_user = models.CharField(
        verbose_name='数据库用户', max_length=100, default='postgres', blank=True)
    db_password = models.CharField(verbose_name='数据库密码', max_length=250, blank=True, default='initial',
                                   help_text='安装的默认密码是initial')
    status = models.IntegerField(verbose_name='安装状态', help_text="数据库是否配置好了", choices=STATUS_CHOICE,
                                 default=-1)
    query_user = models.CharField(
        '数据库查询账号', max_length=100, default='readonly')
    query_password = models.CharField(
        '数据库查询密码', max_length=100, blank=True, help_text='不可以修改')

    create_date = models.DateTimeField('创建时间', auto_now_add=True)
    update_date = models.DateTimeField('修改时间', auto_now_add=True)
    platform = models.ForeignKey(PlatForm, verbose_name='对应平台')

    class Meta:
        ordering = ['-create_date']
        # order_with_respect_to
        unique_together = ('server', 'port')

    def __str__(self):
        return '%s:%s' % (self.server, self.port)


class Mongodb(models.Model):
    MASTER = 'master'
    SLAVE = 'standby'
    ARCHITECTURE_CHOICES = (
        (MASTER, '主数据库'),
        (SLAVE, '从数据库')
    )
    server = models.ForeignKey(Server, verbose_name='服务器')
    port = models.IntegerField('端口', default=3306)
    mongo_type = models.CharField(
        verbose_name='mysql类型', choices=ARCHITECTURE_CHOICES, max_length=100)
    master = models.ForeignKey('self', verbose_name='主数据库', blank=True, null=True,
                               limit_choices_to={'mongo_type': 'master'})
    db_user = models.CharField(
        verbose_name='数据库用户', max_length=100, default='postgres', blank=True)
    db_password = models.CharField(verbose_name='数据库密码', max_length=250, blank=True, default='initial',
                                   help_text='安装的默认密码是initial')
    status = models.IntegerField(verbose_name='安装状态', help_text="数据库是否配置好了", choices=STATUS_CHOICE,
                                 default=-1)
    query_user = models.CharField(
        '数据库查询账号', max_length=100, default='readonly')
    query_password = models.CharField(
        '数据库查询密码', max_length=100, blank=True, help_text='不可以修改')

    create_date = models.DateTimeField('创建时间', auto_now_add=True)
    update_date = models.DateTimeField('修改时间', auto_now_add=True)
    platform = models.ForeignKey(PlatForm, verbose_name='对应平台', blank=True,)

    class Meta:
        ordering = ['-create_date']
        # order_with_respect_to
        unique_together = ('server', 'port')

    def __str__(self):
        return '%s:%s' % (self.server, self.port)
